package cn.tedu.submarine;
import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class World extends JPanel{
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    public static final int START = 0;
    public static final int RUNNING = 1;
    public static final int GAME_OVER = 2;
    private int state = START;//默认是活着的

    private Battleship ship = new Battleship();//战舰对象
    private SeaObject[] submarines = {};
    private SeaObject[] thunders = {};
    private Bomb[] bombs = {};
    private  SeaObject[] rt = {};


    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }


    private int subEnterIndex = 0;
    public void submarineEnterAction(){
        subEnterIndex++;
          if(subEnterIndex%40==0){
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;

        }
    }

    private int backEnterAction = 0;
    public void backEnterAction(){
        backEnterAction++;
        if(backEnterAction%100==0){
            for (int i = 0; i < submarines.length; i++) {
              if(submarines[i].isSecond()){
                  submarines[i].goDead();
              }
            }
        }
    }


    private int thunderEnterAction = 0;
    public void thunderEnterAction(){
        thunderEnterAction++;
        if(thunderEnterAction%100==0){
            for (int i = 0; i<submarines.length; i++) {
                SeaObject obj = submarines[i].shootThunder();

               if(obj!=null) {
                   thunders = Arrays.copyOf(thunders, thunders.length + 1);
                   thunders[thunders.length - 1] = obj;
               }
            }
        }

    }


    public void stepAction(){
        for (int i = 0; i <submarines.length ; i++) {
            submarines[i].step();
        }
        for (int i = 0; i <thunders.length ; i++) {
            thunders[i].step();
        }
        for (int i = 0; i <bombs.length/3 ; i++) {
            bombs[3*i].step();
            bombs[3*i+1].a();
            bombs[3*i+2].b();
        }
    }



    public void bombEnterAction(){
        Bomb[] obj = ship.shootBomb();
        bombs = Arrays.copyOf(bombs,bombs.length+3);
        bombs[bombs.length-1] = obj[0];
        bombs[bombs.length-2] = obj[1];
        bombs[bombs.length-3] = obj[2];
    }


    public void outOfBoundsAction(){
        for (int i = 0; i <submarines.length ; i++) {
            if (submarines[i].isOutOfBounds() || submarines[i].isDead() ||submarines[i].isSecond()) {
                submarines[i] = submarines[submarines.length - 1];
                submarines = Arrays.copyOf(submarines, submarines.length - 1);
            }
        }

        for(int i=0;i<bombs.length/3;i++) {
            if (bombs[i].isOutOfBounds() ) {
                bombs[i] = bombs[bombs.length - 3];
                bombs[i+1] = bombs[bombs.length - 2];
                bombs[i+2] = bombs[bombs.length -1];
                bombs = Arrays.copyOf(bombs, bombs.length - 3);
            }
        }
        for(int i=0;i<thunders.length;i++) {
            if (thunders[i].isOutOfBounds()) {
                thunders[i] = thunders[thunders.length - 1];
                thunders = Arrays.copyOf(thunders, thunders.length - 1);
            }
        }


    }


    /**炸弹与潜艇的碰撞*/
    private int score;
    public void bombBangAction(){//定时发生,每10ms走一次
        for (int i = 0; i < bombs.length; i++) {
            Bomb b = bombs[i];
            for (int j = 0; j<submarines.length; j++){
                SeaObject s = submarines[j];
                if(b.isLive() && s.isLive() && s.isHit(b)){
                    b.goDead();
                    if(s instanceof ObserveSubmarine){
                        s.goSecond();
                        ObserveSubmarine os = (ObserveSubmarine)s;
                        score+=os.getScore();
                    }else if(s instanceof EnemyScore){
                        s.goDead();
                        EnemyScore es = (EnemyScore)s;
                        score+=es.getScore();
                    }
                    if(s instanceof EnemyLife){
                        s.goDead();
                        EnemyLife el = (EnemyLife)s;
                        ship.addLife(el.getLife());
                    }
                }
            }
        }
    }


    /**雷和战舰的碰撞*/
    public void thunderBangAction(){
        for (int i = 0; i <thunders.length ; i++) {
            SeaObject s = thunders[i];
            if(ship.isLive() && s.isLive() && s.isHit(ship)){
                s.goDead();
                ship.subtractLife();
            }
        }
    }

    public void checkGameOverAction(){
        if(ship.getLife()<=0){
            state = GAME_OVER;
        }
    }


    public void action(){
        KeyAdapter k = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_SPACE){//按下空格键
                    switch (state){
                        case START:
                            state = RUNNING;
                            break;
                        case RUNNING:
                            bombEnterAction();
                            break;
                        case GAME_OVER:
                            score = 0;
                            ship = new Battleship();
                            thunders = new SeaObject[0];
                            submarines = new SeaObject[0];
                            bombs = new Bomb[0];
                            state = START;
                    }

                }else if(e.getKeyCode()==KeyEvent.VK_RIGHT && state==RUNNING){
                    ship.moveRight();
                }else if(e.getKeyCode()==KeyEvent.VK_LEFT && state==RUNNING){
                    ship.moveLeft();
                }else if(e.getKeyCode()==KeyEvent.VK_DOWN && state==RUNNING){
                    ship.moveDown();
                }else if(e.getKeyCode()==KeyEvent.VK_UP && state==RUNNING){
                    ship.moveUp();
                }
            }
        };
        this.addKeyListener(k);


        Timer timer = new Timer();
        int interval = 10;//定时间隔(以毫秒为单位)
        timer.schedule(new TimerTask() {
            public void run() {
                if(state==RUNNING) {
                    submarineEnterAction();
                    thunderEnterAction();
                    stepAction();
                    outOfBoundsAction();
                    bombBangAction();
                    thunderBangAction();
                    backEnterAction();
                    checkGameOverAction();
                }
                repaint();
              }
        }, interval, interval);

    }


    public void paint(Graphics g){
        switch (state){
            case START:
                Images.start.paintIcon(null,g,0,0);
                break;
            case RUNNING:
                Images.sea.paintIcon(null,g,0,0);
                ship.paintImage(g);
                for (int i = 0; i <submarines.length;i++) {
                    submarines[i].paintImage(g);
                }
                for (int i = 0; i <thunders.length;i++) {
                    thunders[i].paintImage(g);
                }
                for (int i = 0; i <bombs.length;i++) {
                    bombs[i].paintImage(g);
                }
                g.drawString("SCORE:"+score,250,50);
                g.drawString("LIFE:"+ship.getLife(),400,50);
                break;
            case GAME_OVER:
                Images.gameover.paintIcon(null,g,0,0);
                break;
        }

    }


    public void paintWorld(){ 
        JFrame frame = new JFrame();//新建对象
        this.setFocusable(true);//键盘输入
        frame.add(this);//添加窗口
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使关闭时程序停止
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);//使窗口居中
        frame.setVisible(true);//设置窗口的可见性,尽快的调用paint()方法
    }


    public static void main(String[] args) {
        World w = new World();
        w.paintWorld();
        w.action();


    }


}
